 package queue_model;

import simpackNew.Const;
import estatistica.Dispersion;

public class Simulacao {
    public static int simulationNumber = 5;
    
    // int do_preempt = Const.true_;
    public static int do_preempt = Const.false_;
    
    public static int[] percentages = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95};				// Percentage of ClassA in the system
    public static int[] contracts = {5, 10, 20, 30, 40, 50}; 			// Variation of contracts in relation to FIFO
    public static int amountOfSceneries = percentages.length * contracts.length;
    public static int scenery;							// Scenery descriptor
	
    public static double mRt = 0.0;							// Mean residence time (simpack report)
    public static double contractA = 0.0;
    public static double contractB = 0.0;
    
    public static double[][][] allFulfilledContractsA = new double[Const.N_POLITICS][amountOfSceneries][simulationNumber * Const.N_USERS_A];
    public static double[][][] allFulfilledContractsB = new double[Const.N_POLITICS][amountOfSceneries][simulationNumber * Const.N_USERS_B];

    public static double[][][] allLatenciesBelowAverageA = new double[Const.N_POLITICS][amountOfSceneries][simulationNumber * Const.N_USERS_A];
    public static double[][][] allLatenciesBelowAverageB = new double[Const.N_POLITICS][amountOfSceneries][simulationNumber * Const.N_USERS_B];

    
        
    public static void simulateCase(QueueModel test, int politicType, int simID, int perc, int cVar) {
	    // ************** Simulate Politics **************
	    test = new QueueModel(politicType, do_preempt, simID, perc, cVar, contractA, contractB);
	    test.simulate();

	    // ClassA data update
	    System.arraycopy(test.fulfilledContracts, 0, allFulfilledContractsA[politicType][scenery], ((simID - 1) * Const.N_USERS_A), Const.N_USERS_A);
	    System.arraycopy(test.latenciesBelowAverage, 0, allLatenciesBelowAverageA[politicType][scenery], ((simID - 1) * Const.N_USERS_A), Const.N_USERS_A);
	    
	    // ClassB data update
	    System.arraycopy(test.fulfilledContracts, Const.N_USERS_B, allFulfilledContractsB[politicType][scenery], ((simID - 1) * Const.N_USERS_B), Const.N_USERS_B);
	    System.arraycopy(test.latenciesBelowAverage, Const.N_USERS_B, allLatenciesBelowAverageB[politicType][scenery],((simID - 1) * Const.N_USERS_B), Const.N_USERS_B);

    }

    public static void main(String[] args) {
	
	/**
	 * IMPORTANT: the amount of politics to be simulated must be defined by Const.N_POLITICS
	 * */
	for (int n = 1; n <= simulationNumber; n++) {
	    scenery = 0;
	    for (int p : percentages) {
		// Simulate FIFO, just to obtain the mean residence time
		QueueModel test = new QueueModel(Const.FIFO, do_preempt, n, p, 0, contractA, contractB);
		mRt = test.simulate();
		
		
		for (int c : contracts) {
		    contractA = (mRt - ((0.01 * c) * mRt));
		    contractB = (mRt + ((0.01 * c ) * mRt));
		    
		    // ************** FIFO **************
		    //simulateCase(test, Const.FIFO, n, p, c);
		    
		    // ************** EBS Ultima Abordagem **************
		    simulateCase(test, Const.EBS, n, p, c);
		    
		    // ************** SJF **************
		    //simulateCase(test, Const.SJF, n, p, c);

		    
		    scenery++;
		}
	    }
	}

	// Calculo Satisfação e Latências abaixo média
//	scenery = 0;
//	for (int p : percentages)
//	    for (int c : contracts) {
//		// FIFO
//		Dispersion.calculateDeviation(allFulfilledContractsA[Const.FIFO][scenery], p, c, "A", "FIFO", Const.FULFILMENT_PERCENTAGE);
//		Dispersion.calculateDeviation(allFulfilledContractsB[Const.FIFO][scenery], p, c, "B", "FIFO", Const.FULFILMENT_PERCENTAGE);
//
//		Dispersion.calculateDeviation(allLatenciesBelowAverageA[Const.FIFO][scenery], p, c, "A", "FIFO", Const.LATENCIES_BELOW_AVERAGE);
//		Dispersion.calculateDeviation(allLatenciesBelowAverageB[Const.FIFO][scenery], p, c, "B", "FIFO", Const.LATENCIES_BELOW_AVERAGE);
//
//		
//		// EBS
//		Dispersion.calculateDeviation(allFulfilledContractsA[Const.EBS][scenery], p, c, "A", "1Abordagem_EBS", Const.FULFILMENT_PERCENTAGE);
//		Dispersion.calculateDeviation(allFulfilledContractsB[Const.EBS][scenery], p, c, "B", "1Abordagem_EBS", Const.FULFILMENT_PERCENTAGE);
//		
//		Dispersion.calculateDeviation(allLatenciesBelowAverageA[Const.EBS][scenery], p, c, "A", "1Abordagem_EBS", Const.LATENCIES_BELOW_AVERAGE);
//		Dispersion.calculateDeviation(allLatenciesBelowAverageB[Const.EBS][scenery], p, c, "B", "1Abordagem_EBS", Const.LATENCIES_BELOW_AVERAGE);
//
//		
//		// SJF
//		Dispersion.calculateDeviation(allFulfilledContractsA[Const.SJF][scenery], p, c, "A", "SJF", Const.FULFILMENT_PERCENTAGE);
//		Dispersion.calculateDeviation(allFulfilledContractsB[Const.SJF][scenery], p, c, "B", "SJF", Const.FULFILMENT_PERCENTAGE);
//		
//		Dispersion.calculateDeviation(allLatenciesBelowAverageA[Const.SJF][scenery], p, c, "A", "SJF", Const.LATENCIES_BELOW_AVERAGE);
//		Dispersion.calculateDeviation(allLatenciesBelowAverageB[Const.SJF][scenery], p, c, "B", "SJF", Const.LATENCIES_BELOW_AVERAGE);
//
//		scenery++;
//	    }
//
    }
}